package pfc;

import edu.stanford.smi.protege.model.Cls;
import edu.stanford.smi.protege.model.Instance;
import edu.stanford.smi.protege.model.Project;
import edu.stanford.smi.protegex.owl.model.impl.DefaultRDFIndividual;
import edu.stanford.smi.protegex.owl.ui.ResourceRenderer;
import java.awt.Dimension;
import java.util.Iterator;
import javax.swing.DefaultListModel;
import javax.swing.JComponent;
import javax.swing.plaf.basic.BasicInternalFrameUI;

/**
 *
 * @author <Mejico, Rocío; Mignola, Natalia; Ordoñez, Agustina>
 */
public class PantallaLEL extends javax.swing.JInternalFrame {

    private static Project prj;

    /** Creates new form pantallaLELs */
    public PantallaLEL() {
        initComponents();
    }

    public PantallaLEL(Project pro)  {
        initComponents();
        prj = pro;
        this.cargarLista("Estate");
        this.quitarBarraSuperior();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        listaTipoSimbolos = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        listaSimbolos = new javax.swing.JList();
        panelCentral = new javax.swing.JPanel();
        btNuevoSimbolo = new javax.swing.JButton();
        btEliminarSimbolo = new javax.swing.JButton();

        setBorder(javax.swing.BorderFactory.createTitledBorder("LELs"));
        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setPreferredSize(new java.awt.Dimension(401, 580));

        listaTipoSimbolos.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Estado", "Objeto", "Sujeto", "Verbo" }));
        listaTipoSimbolos.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                listaTipoSimbolosActionPerformed(evt);
            }
        });

        listaSimbolos.setBackground(new java.awt.Color(236, 233, 216));
        listaSimbolos.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        listaSimbolos.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                listaSimbolosValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(listaSimbolos);

        panelCentral.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        panelCentral.setPreferredSize(new java.awt.Dimension(401, 277));

        javax.swing.GroupLayout panelCentralLayout = new javax.swing.GroupLayout(panelCentral);
        panelCentral.setLayout(panelCentralLayout);
        panelCentralLayout.setHorizontalGroup(
            panelCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 361, Short.MAX_VALUE)
        );
        panelCentralLayout.setVerticalGroup(
            panelCentralLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 328, Short.MAX_VALUE)
        );

        btNuevoSimbolo.setText("Nuevo");
        btNuevoSimbolo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btNuevoSimboloActionPerformed(evt);
            }
        });

        btEliminarSimbolo.setText("Eliminar");
        btEliminarSimbolo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btEliminarSimboloActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(panelCentral, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 365, Short.MAX_VALUE))
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGap(38, 38, 38)
                            .addComponent(listaTipoSimbolos, javax.swing.GroupLayout.PREFERRED_SIZE, 316, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(109, 109, 109)
                        .addComponent(btNuevoSimbolo, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btEliminarSimbolo)))
                .addContainerGap())
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btEliminarSimbolo, btNuevoSimbolo});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(listaTipoSimbolos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(16, 16, 16)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(panelCentral, javax.swing.GroupLayout.PREFERRED_SIZE, 332, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btNuevoSimbolo)
                    .addComponent(btEliminarSimbolo, javax.swing.GroupLayout.PREFERRED_SIZE, 23, javax.swing.GroupLayout.PREFERRED_SIZE)))
        );

        layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btEliminarSimbolo, btNuevoSimbolo});

        pack();
    }// </editor-fold>

    private void listaSimbolosValueChanged(javax.swing.event.ListSelectionEvent evt) {
        if (!this.listaSimbolos.isSelectionEmpty()) {
            Instance iObj = (Instance) this.listaSimbolos.getModel().getElementAt(this.listaSimbolos.getSelectedIndex());
            if (iObj == null) {
                this.panelCentral.setEnabled(false);
                this.panelCentral.removeAll();
                this.panelCentral.revalidate();
                return;
            }

            btEliminarSimbolo.setEnabled(true);
            JComponent insPanel = (JComponent) prj.createRuntimeClsWidget(iObj);
            panelCentral.removeAll();
            panelCentral.add(insPanel);
            insPanel.setBounds(1, 1, 360, 300);
            insPanel.setVisible(true);
            panelCentral.repaint();
            panelCentral.revalidate();

            if (listaSimbolos.getValueIsAdjusting()) {
                return;
            }
        } else {
            this.panelCentral.removeAll();
            this.panelCentral.repaint();
        }
}

    /*carga la lista de individuos de protege según el tipo*/
    private void cargarLista(String tipo) {
        DefaultListModel listModel = new DefaultListModel();

        if (!prj.getKnowledgeBase().getInstances(prj.getKnowledgeBase().getCls("http://www.owl-ontologies.com/SE.RE.Elic.LeL.owl#" + tipo)).isEmpty()) {
            for (Iterator<Instance> it = prj.getKnowledgeBase().getInstances(prj.getKnowledgeBase().getCls("http://www.owl-ontologies.com/SE.RE.Elic.LeL.owl#" + tipo)).iterator(); it.hasNext();) {
                DefaultRDFIndividual individual = (DefaultRDFIndividual) it.next();
                listModel.addElement(individual);
            }
            this.listaSimbolos.setModel(listModel);
            this.listaSimbolos.setCellRenderer(new ResourceRenderer());
            this.listaSimbolos.clearSelection();
            this.listaSimbolos.setSelectedIndex(listaSimbolos.getLastVisibleIndex());

        } else {
            listModel.clear();
            this.listaSimbolos.setModel(listModel);
        }
    }

    private void agregarSimbolo() {
        String btSeleccionado = this.obtenerTipoSeleccionado();
        Cls cls = prj.getKnowledgeBase().getCls("http://www.owl-ontologies.com/SE.RE.Elic.LeL.owl#" + btSeleccionado);
        String instName = btSeleccionado + prj.getKnowledgeBase().getInstanceCount(cls); //agrega por ej: Verbo2 y así..
        prj.getKnowledgeBase().createInstance(instName, cls);
        cargarLista(btSeleccionado);
    }

    private void eliminarSimbolo(String tipo) {
        DefaultListModel lista = new DefaultListModel();

        for (Iterator<Instance> it = prj.getKnowledgeBase().getInstances(prj.getKnowledgeBase().getCls("http://www.owl-ontologies.com/SE.RE.Elic.LeL.owl#" + tipo)).iterator(); it.hasNext();) {
            DefaultRDFIndividual individual = (DefaultRDFIndividual) it.next(); //antes OWLIndividual despues: DefaultRDFIndividual
            lista.addElement(individual);
        }

        for (int i = 0; i < lista.size(); i++) {
            if (this.listaSimbolos.getSelectedValue().equals(lista.get(i))) {
                Instance iObj = (Instance) this.listaSimbolos.getModel().getElementAt(this.listaSimbolos.getSelectedIndex());
                prj.getKnowledgeBase().deleteInstance(iObj);
                lista.removeElementAt(i);
                break;
            }
        }
        this.listaSimbolos.setModel(lista);
        this.listaSimbolos.setCellRenderer(new ResourceRenderer());
        this.listaSimbolos.clearSelection();
    }

    private String obtenerTipoSeleccionado() {
        if (this.listaTipoSimbolos.getSelectedItem().equals("Estado")) {
            return new String("Estate");
        }
        if (this.listaTipoSimbolos.getSelectedItem().equals("Objeto")) {
            return new String("Object");
        }
        if (this.listaTipoSimbolos.getSelectedItem().equals("Sujeto")) {
            return new String("Subject");
        }
        if (this.listaTipoSimbolos.getSelectedItem().equals("Verbo")) {
            return new String("Verb");
        }
        return null;
    }

    private void quitarBarraSuperior() {
        BasicInternalFrameUI ui = (BasicInternalFrameUI) this.getUI();
        ui.getNorthPane().setPreferredSize(new Dimension(0, 0));
    }


    /*ACCIONES DE BOTONES y LISTA*/
    private void btNuevoSimboloActionPerformed(java.awt.event.ActionEvent evt) {
        this.agregarSimbolo();
}

    private void btEliminarSimboloActionPerformed(java.awt.event.ActionEvent evt) {
        if (this.listaSimbolos.isSelectionEmpty()) {
            javax.swing.JOptionPane.showMessageDialog(null, "Debe seleccionar un elemento a eliminar");
        } else {
            this.eliminarSimbolo(this.obtenerTipoSeleccionado());
        }
}

    private void listaTipoSimbolosActionPerformed(java.awt.event.ActionEvent evt) {
        this.cargarLista(this.obtenerTipoSeleccionado());
    }
    // Variables declaration - do not modify
    private javax.swing.JButton btEliminarSimbolo;
    private javax.swing.JButton btNuevoSimbolo;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JList listaSimbolos;
    private javax.swing.JComboBox listaTipoSimbolos;
    private javax.swing.JPanel panelCentral;
    // End of variables declaration
}
